<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_type_model extends Mongo_model {
	
    protected $_table_name = 'admin_type';
	
	
	public function __construct()
    {
        parent::__construct();
    }
	
	/**
	 * 获得拥有全部权限的检测
	 *
	 * @id 角色id
	 */
	public function get_all_root($id)
	{
		if(empty($id))
		{
			return false;
		}
		$this->mongo_db->where('id',$id);
		$this->mongo_db->where('dtime',0);
		$info = $this->get_once();
		if($info == null)
		{
			return  false;
		}
		if( $info['type_root'] == null)
		{
			return false;
		}
		$info_arr = explode(',',$info['type_root'] );
		foreach($info_arr as $val)
		{
			if($val == '#all')
			{
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 获得权限数组
	 *
	 * @id 角色id
	 */
	public function get_root_array($id)
	{
		if(empty($id))
		{
			return null;
		}
		$this->mongo_db->where('id',$id);
		$this->mongo_db->where('dtime',0);
		$info = $this->get_once();
		if($info == null)
		{
			return  null;
		}
		if($info['type_root'] == null)
		{
			return null;
		}
		$info_arr = explode(',',$info['type_root'] );
		$info_arr = @array_filter($info_arr);
		return $info_arr;
	}
	
	public function get_role_info($id = '')
	{
		$this->mongo_db->where('id',$id);
		return $this->get_once();
	}
	
	public function get_admin_type_like($limit = 0 ,$keyword = NULL)
	{
		$data_info['dtime'] = 0;
		$this->where($data_info);
		$this->set_like('id',$keyword);
		
		if($this->set_limit($limit))
		{
			return $this->get();
		}
		else
		{
			return $this->count();
		}
		
	}
	
	public function get_admin_type_info($id = '')
	{
		if(empty($id))
		{
			return false;
		}
		$data_info['id'] = $id;
		$data_info['dtime'] = 0;
		$this->mongo_db->where($data_info);
		return $this->get_once();
	}
	
	public function update_type($id = 0 ,$date = null)
	{
		if(empty($id)||empty($date))
		{
			return false;
		}
		$date['mtime'] = time();
		$this->mongo_db->set($date);
		$this->mongo_db->where('id' , $id);
		return $this->update();
	}
	
	public function insert_type($date = null)
	{
		if(empty($date))
		{
			return false;
		}
		$date['ctime'] = time();
		$date['mtime'] = 0;
		$date['dtime'] = 0;
		$date['id'] = $this->get_no('admin_type_');
		
		return $this->insert($date);
	}
	
	public function delect_type($id)
	{
		if(empty($id))
		{
			return false;
		}
		$this->mongo_db->where('id', $id);
		
		return $this->update('dtime',time() );
	}
	
	public function get_name_array()
	{
		$date = array(''=>'请选择');
		$this->mongo_db->where('dtime',0);
		$info_array = $this->get();
		if($info_array == null)
		{
			return $date;
		}
		foreach($info_array as $info)
		{
			$date[$info['id']] = $info['name'];
		}
		return $date;
	}
}